package cn.china.xfood.controller;


import entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RedisController {

    @Autowired
    ValueOperations<String, Object> valueOperations;

    @Autowired
    HashOperations<String, String, Object> hashOperations;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("/setString")
    public void setString(HttpServletResponse res){
        res.setHeader("Access-Control-Allow-Origin","*");

//        valueOperations.set("hello","world");
        System.out.println(valueOperations.get("h&m"));
    }


    @RequestMapping("/setMap")
    public void setMap(){
        hashOperations.put("map","name","tom");
        hashOperations.put("map","age","16");

        Map<String,String> map1 = new HashMap<>();
        map1.put("name","Jip");
        map1.put("age","20");
        hashOperations.putAll("map2",map1);
        System.out.println("ok");
    }

    @RequestMapping("/getMap")
    public  void getMap(){
        Map<String, Object> map1 = hashOperations.entries("map2");
        System.out.println(map1);
    }

    @RequestMapping("/setList")
    public void setList(){
//        redisTemplate.opsForList().leftPush("a","1");

        List<String> list = new ArrayList<>();
        list.add("z");
        list.add("oo");
        list.add("e");
        redisTemplate.opsForList().leftPushAll("listz",list);
    }

    @RequestMapping("/getList")
    public void getList(){
        List<Object> list = redisTemplate.opsForList().range("listz", 0, redisTemplate.opsForList().size("listz") - 1);
        System.out.println(list);
    }




    @RequestMapping("/setStudent1")
    public int setStudent(){
        Student student = new Student("kaor1",21,"男");
        redisTemplate.opsForList().leftPush("stu",student);
        List<Object> stu = redisTemplate.opsForList().range("stu", 0, redisTemplate.opsForList().size("stu") - 1);
        System.out.println(stu);


        return 1;
        //返回到调用这个函数的地方,浏览器界面
    }

    @RequestMapping("/setStudent2")
    public int setStudent2(){

        List<Object> stu = redisTemplate.opsForList().range("stu", 0, redisTemplate.opsForList().size("stu") - 1);
        Student student = new Student("kaor2",21,"男");
        Student student1 = new Student("kaor3",21,"女");
        stu.add(student);stu.add(student1);
        redisTemplate.opsForList().getOperations().delete("stu");
        redisTemplate.opsForList().leftPushAll("stu",stu);

        return 1;//返回到调用这个函数的地方,浏览器界面

    }



    @RequestMapping("/getStudent")
    public void getStudent(){
        List<Object> student = redisTemplate.opsForList().range("stupid", 0, redisTemplate.opsForList().size("stupid") - 1);
        System.out.println(student);
        if(student.isEmpty()){
            System.out.println("nice");
        }

        Student student1 = new Student("kaor2",21,"男");
        Student student2 = new Student("kaor3",21,"女");

       student.add(student1);student.add(student2);

        redisTemplate.opsForList().leftPushAll("stupid",student);
    }

    @RequestMapping("updateStudent")
    public void update(){
        List<Object> range = redisTemplate.opsForList().range("stu", 0, redisTemplate.opsForList().size("stu") - 1);

        for (int i = 0; i < range.size() ; i++) {
            Student student = (Student) range.get(i);
            if("kaor2".equals(student.getName())){
                student.setName("hello");
            }
        }

        redisTemplate.opsForList().getOperations().delete("stu");
        redisTemplate.opsForList().leftPushAll("stu",range);
        System.out.println(1);
    }


    @RequestMapping("setSet")
    public Integer setSet(){
        redisTemplate.opsForSet().add("s1","1");
        redisTemplate.opsForSet().add("s1","2");
        List<String> col = new ArrayList<>();
        col.add("1");
        col.add("2");
        col.add("3");
        redisTemplate.opsForSet().intersect("s2",col);
        return 1;

    }

}
